//
//  HNWebViewController.m
//  JHFBaseProject
//
//  Created by 朱传正 on 2016/12/14.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNWebViewController.h"

@interface HNWebViewController ()<UIWebViewDelegate>

@end

@implementation HNWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];


        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,60, kScreen_Width, kScreen_Height-60)];
        _webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    //    [_webView loadRequest:_req];
        // 应用场景:加载从服务器上下载的文件,例如pdf,或者word,图片等等文件
//        NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];
//        NSString *filePath  = [resourcePath stringByAppendingPathComponent:@"login.html"];
//        NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//        [self.webView loadHTMLString:htmlstring  baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];
    
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"];
    NSString *fileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:fileContent baseURL:[NSURL URLWithString:filePath]];
    
    
    
    _webView.scrollView.bounces = NO;
    _webView.delegate = self;
    [_webView setBackgroundColor:[UIColor colorWithHexString:@"#ececec"]];
    [self.view addSubview:_webView];
}

#pragma -mark WebView
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"WebView erros is %@",error);
    if ([error code] == NSURLErrorCancelled) {
        return;
    }
    // 应用场景:加载从服务器上下载的文件,例如pdf,或者word,图片等等文件
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"error" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    // Disable user selection
    //    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    //    // Disable callout
    //    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
    
}
- (BOOL)webView: (UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    //    //注册JS
    //    JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //    context[@"Product"]=self;
    
//    NSString *_str = [[request URL] absoluteString];
//    NSLog(@"=============123:%@",request);
//    if ([[_str substringToIndex:5] isEqualToString:@"file:"]) {
//        return YES;
//    }else if ([[_str substringToIndex:5] isEqualToString:@"http:"]) {
//        return NO;
//    }
    return YES;
}

@end
